home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.plaf.basic;
-
- import com.sun.java.swing.BoxLayout;
- import com.sun.java.swing.JComponent;
- import com.sun.java.swing.JFrame;
- import com.sun.java.swing.JToolBar;
- import com.sun.java.swing.LookAndFeel;
- import com.sun.java.swing.UIManager;
- import com.sun.java.swing.plaf.ComponentUI;
- import com.sun.java.swing.plaf.ToolBarUI;
- import com.sun.java.swing.plaf.UIResource;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.Frame;
- import java.awt.Point;
- import java.awt.Window;
- import java.awt.event.WindowListener;
- import java.beans.PropertyChangeEvent;
- import java.io.Serializable;
-
- public class BasicToolBarUI extends ToolBarUI implements Serializable {
- protected JToolBar toolBar;
- private boolean floating;
- private boolean floatable;
- private int floatingX;
- private int floatingY;
- private JFrame floatingFrame;
- protected DragWindow dragWindow;
- private Container dockingSource;
- private DockingListener dockingListener;
- private int dockingSensitivity = 0;
- protected Color dockingColor = null;
- protected Color floatingColor = null;
- protected Color dockingBorderColor = null;
- protected Color floatingBorderColor = null;
- public static final int HORIZONTAL = 0;
- public static final int VERTICAL = 1;
-
- public boolean canDock(Component c, Point p) {
- boolean b = false;
- if (c.contains(p)) {
- if (this.dockingSensitivity == 0) {
- this.dockingSensitivity = this.toolBar.getSize().height;
- }
-
- if (p.y < this.dockingSensitivity) {
- b = true;
- }
-
- if (p.y > c.getSize().height - this.dockingSensitivity) {
- b = true;
- }
-
- if (p.x < this.dockingSensitivity) {
- b = true;
- }
-
- if (p.x > c.getSize().width - this.dockingSensitivity) {
- b = true;
- }
- }
-
- return b;
- }
-
- protected DockingListener createDockingListener(JToolBar toolbar) {
- return new DockingListener(this, toolbar);
- }
-
- protected DragWindow createDragWindow(JToolBar toolbar) {
- Frame frame = null;
- if (this.toolBar != null) {
- Container p;
- for(p = this.toolBar.getParent(); p != null && !(p instanceof Frame); p = ((Component)p).getParent()) {
- }
-
- if (p != null && p instanceof Frame) {
- frame = (Frame)p;
- }
- }
-
- if (frame == null) {
- this.floatingFrame = this.createFloatingFrame(this.toolBar);
- frame = this.floatingFrame;
- }
-
- DragWindow dragWindow = new DragWindow(frame);
- return dragWindow;
- }
-
- protected JFrame createFloatingFrame(JToolBar toolbar) {
- JFrame frame = new JFrame(((Component)toolbar).getName());
- WindowListener wl = this.createFrameListener();
- ((Window)frame).addWindowListener(wl);
- return frame;
- }
-
- protected WindowListener createFrameListener() {
- return new FrameListener(this);
- }
-
- public static ComponentUI createUI(JComponent x) {
- return new BasicToolBarUI();
- }
-
- protected void dragTo(Point position, Point origin) {
- if (this.toolBar.isFloatable()) {
- if (this.dragWindow == null) {
- this.dragWindow = this.createDragWindow(this.toolBar);
- }
-
- Point offset = this.dragWindow.getOffset();
- if (offset == null) {
- Dimension size = this.toolBar.getPreferredSize();
- offset = new Point(size.width / 2, size.height / 2);
- this.dragWindow.setOffset(offset);
- }
-
- Point global = new Point(origin.x + position.x, origin.y + position.y);
- Point dragPoint = new Point(global.x - offset.x, global.y - offset.y);
- if (this.dockingSource == null) {
- this.dockingSource = this.toolBar.getParent();
- }
-
- Point dockingPosition = this.dockingSource.getLocationOnScreen();
- Point comparisonPoint = new Point(global.x - dockingPosition.x, global.y - dockingPosition.y);
- if (this.canDock(this.dockingSource, comparisonPoint)) {
- this.dragWindow.setBackground(this.getDockingColor());
- String constraint = this.getDockingConstraint(this.dockingSource, comparisonPoint);
- int orientation = this.mapConstraintToOrientation(constraint);
- this.dragWindow.setOrientation(orientation);
- this.dragWindow.setBorderColor(this.dockingBorderColor);
- } else {
- this.dragWindow.setBackground(this.getFloatingColor());
- this.dragWindow.setOrientation(0);
- this.dragWindow.setBorderColor(this.floatingBorderColor);
- }
-
- this.dragWindow.setLocation(dragPoint.x, dragPoint.y);
- if (!this.dragWindow.isVisible()) {
- Dimension size = this.toolBar.getPreferredSize();
- this.dragWindow.setSize(size.width, size.height);
- this.dragWindow.show();
- }
- }
-
- }
-
- protected void floatAt(Point position, Point origin) {
- if (this.toolBar.isFloatable()) {
- Point offset = this.dragWindow.getOffset();
- if (offset == null) {
- offset = position;
- this.dragWindow.setOffset(position);
- }
-
- Point global = new Point(origin.x + position.x, origin.y + position.y);
- this.setFloatingLocation(global.x - offset.x, global.y - offset.y);
- if (this.dockingSource != null) {
- Point dockingPosition = this.dockingSource.getLocationOnScreen();
- Point comparisonPoint = new Point(global.x - dockingPosition.x, global.y - dockingPosition.y);
- if (this.canDock(this.dockingSource, comparisonPoint)) {
- this.setFloating(false, comparisonPoint);
- } else {
- this.setFloating(true, (Point)null);
- }
- } else {
- this.setFloating(true, (Point)null);
- }
-
- this.dragWindow.setOffset((Point)null);
- }
-
- }
-
- public Color getDockingColor() {
- return this.dockingColor;
- }
-
- private String getDockingConstraint(Component c, Point p) {
- String s = "North";
- if (p != null && c.contains(p)) {
- if (this.dockingSensitivity == 0) {
- this.dockingSensitivity = this.toolBar.getSize().height;
- }
-
- if (p.y > c.getSize().height - this.dockingSensitivity) {
- s = "South";
- }
-
- if (p.x < this.dockingSensitivity) {
- s = "West";
- }
-
- if (p.x > c.getSize().width - this.dockingSensitivity) {
- s = "East";
- }
-
- if (p.y < this.dockingSensitivity) {
- s = "North";
- }
- }
-
- return s;
- }
-
- public Color getFloatingColor() {
- return this.floatingColor;
- }
-
- public Dimension getMaximumSize(JComponent c) {
- return this.getPreferredSize(c);
- }
-
- public Dimension getMinimumSize(JComponent c) {
- return this.getPreferredSize(c);
- }
-
- public Dimension getPreferredSize(JComponent c) {
- return null;
- }
-
- protected void installDefaults(JComponent c) {
- LookAndFeel.installBorder(c, "ToolBar.border");
- LookAndFeel.installColorsAndFont(c, "ToolBar.background", "ToolBar.foreground", "ToolBar.font");
- if (this.dockingColor == null || this.dockingColor instanceof UIResource) {
- this.dockingColor = UIManager.getColor("ToolBar.dockingColor");
- }
-
- if (this.floatingColor == null || this.floatingColor instanceof UIResource) {
- this.floatingColor = UIManager.getColor("ToolBar.floatingColor");
- }
-
- if (this.dockingBorderColor == null || this.dockingBorderColor instanceof UIResource) {
- this.dockingBorderColor = UIManager.getColor("ToolBar.dockingBorderColor");
- }
-
- if (this.floatingBorderColor == null || this.floatingBorderColor instanceof UIResource) {
- this.floatingBorderColor = UIManager.getColor("ToolBar.floatingBorderColor");
- }
-
- }
-
- protected void installListeners(JComponent c) {
- this.dockingListener = this.createDockingListener((JToolBar)c);
- this.setFloatable(true);
- }
-
- public void installUI(JComponent c) {
- this.toolBar = (JToolBar)c;
- this.installDefaults(c);
- this.dockingSensitivity = 0;
- this.floating = this.floatable = false;
- this.floatingX = this.floatingY = 0;
- this.floatingFrame = this.createFloatingFrame(this.toolBar);
- this.setOrientation(0);
- this.installListeners(c);
- c.setOpaque(true);
- }
-
- public boolean isFloating() {
- return this.floating;
- }
-
- private int mapConstraintToOrientation(String constraint) {
- int orientation = 0;
- if (constraint != null && (constraint.equals("East") || constraint.equals("West"))) {
- orientation = 1;
- }
-
- return orientation;
- }
-
- public void propertyChange(PropertyChangeEvent e) {
- e.getPropertyName();
- if (e.getPropertyName().equals("floatable")) {
- Boolean b = (Boolean)e.getNewValue();
- this.setFloatable(b);
- }
-
- }
-
- public void setDockingColor(Color c) {
- this.dockingColor = c;
- }
-
- public void setFloatable(boolean b) {
- if (b) {
- this.toolBar.addMouseMotionListener(this.dockingListener);
- this.toolBar.addMouseListener(this.dockingListener);
- } else {
- this.toolBar.removeMouseMotionListener(this.dockingListener);
- this.toolBar.removeMouseListener(this.dockingListener);
- }
-
- }
-
- public void setFloating(boolean b, Point p) {
- if (this.toolBar.isFloatable()) {
- if (this.dragWindow != null) {
- this.dragWindow.setVisible(false);
- }
-
- this.floating = b;
- if (b) {
- if (this.dockingSource == null) {
- this.dockingSource = this.toolBar.getParent();
- this.dockingSource.remove(this.toolBar);
- }
-
- if (this.floatingFrame == null) {
- this.floatingFrame = this.createFloatingFrame(this.toolBar);
- }
-
- this.floatingFrame.getContentPane().add(this.toolBar, "Center");
- this.setOrientation(0);
- this.floatingFrame.pack();
- this.floatingFrame.setLocation(this.floatingX, this.floatingY);
- this.floatingFrame.show();
- } else {
- this.floatingFrame.setVisible(false);
- this.floatingFrame.getContentPane().remove(this.toolBar);
- String constraint = this.getDockingConstraint(this.dockingSource, p);
- int orientation = this.mapConstraintToOrientation(constraint);
- this.setOrientation(orientation);
- if (this.dockingSource == null) {
- this.dockingSource = this.toolBar.getParent();
- }
-
- this.dockingSource.add(constraint, this.toolBar);
- }
-
- this.dockingSource.invalidate();
- Container dockingSourceParent = this.dockingSource.getParent();
- if (dockingSourceParent != null) {
- dockingSourceParent.validate();
- }
-
- this.dockingSource.repaint();
- }
-
- }
-
- public void setFloatingColor(Color c) {
- this.floatingColor = c;
- }
-
- public void setFloatingLocation(int x, int y) {
- this.floatingX = x;
- this.floatingY = y;
- }
-
- public void setOrientation(int orientation) {
- if (orientation == 1) {
- this.toolBar.setLayout(new BoxLayout(this.toolBar, 1));
- } else {
- this.toolBar.setLayout(new BoxLayout(this.toolBar, 0));
- }
-
- if (this.dragWindow != null) {
- this.dragWindow.setOrientation(orientation);
- }
-
- }
-
- protected void uninstallDefaults(JComponent c) {
- LookAndFeel.uninstallBorder(c);
- this.dockingColor = null;
- this.floatingColor = null;
- this.dockingBorderColor = null;
- this.floatingBorderColor = null;
- }
-
- protected void uninstallListeners(JComponent c) {
- this.setFloatable(false);
- if (this.dockingListener != null) {
- this.dockingListener = null;
- }
-
- }
-
- public void uninstallUI(JComponent c) {
- this.uninstallDefaults(c);
- if (this.isFloating()) {
- this.setFloating(false, (Point)null);
- }
-
- this.floatingFrame = null;
- this.dragWindow = null;
- this.dockingSource = null;
- this.uninstallListeners(c);
- }
- }
-